<?php

namespace app\api\controller;

use app\api\Model\AdminModel;
use Firebase\JWT\JWT;
use think\Controller;
use think\Db;
use think\facade\Request;
use think\Validate;

class Login extends Cross
{
    public function login(){
        $data = Request::param();
        $admin = new AdminModel();
//        $info = $admin->where('username',$data['username'])->find();
        if (empty($data['username'])){
            return json(['code'=>99,'msg'=>'账号不能不输入']);
        }
        if (empty($data['password'])){
            return json(['code'=>99,'msg'=>'密码不能不输入']);
        }
        $info = Db::table('admin')->where('username',$data['username'])->find();

        if (empty($info)){
            return json(['code'=>0,'msg'=>'找不到该信息']);
        }
        if ($info['password']!=md5($data['password'])){
            return json(['code'=>0,'msg'=>'账号或密码不正确']);
        }
        $jwt = new JWT();

        $key = 'shuai1234';

        $payload = [
            'iss' => 'http://lishuaichuang.com',
            'aud' => 'http://lishuaichuang.com',
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['id']
        ];
        $token = $jwt::encode($payload,$key,'HS256');

        return json(['code'=>1,'msg'=>'正确bulingbuling','token'=>$token]);
    }
    public function a(){
        return '123';
    }
}